package by.grsu.mikhailava.page.tutor;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.mikhailava.app.TestWebApplication;
import by.grsu.mikhailava.dao.SubjectDao;
import by.grsu.mikhailava.entity.Subject;
import by.grsu.mikhailava.entity.User;

public class TutorSubjectsPage extends WebPage{
	
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 4306085419751610514L;
	@SuppressWarnings({ "serial", "rawtypes" })
	public TutorSubjectsPage(User user) {
		super();
	
	final SubjectDao subjectDao = new SubjectDao(TestWebApplication.DATA_XML_FOLDER);

	final List<Subject> allSubject = subjectDao.getAll();

	final User user1 = user;
	add(new DataView<Subject>("table-rows", new ListDataProvider<Subject>(allSubject)) {
		@Override
		protected void populateItem(final Item<Subject> item) {
			final Subject subject = item.getModelObject();

		
			item.add(new Label("name", subject.getSubjectName()));
			item.add(new Label("familiy",user1.getSurName() ));

			item.add(new Link("questions") {
				@Override
				public void onClick() {
					setResponsePage(new QuestionsPage(subject));
				}
			});

			item.add(new Link("deleteSubject") {
				@Override
				public void onClick() {
					subjectDao.delete(subject.getId());
					setResponsePage(new TutorSubjectsPage(user1));
				}
			});
		}
	});

	add(new Link("createSubject") {
		@Override
		public void onClick() {
			setResponsePage(new SubjectCreatePage());
		}
	});
}
}
